package com.example.demo.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.example.demo.mapper.MyAccessTokenMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.MyAccessToken;
import com.example.demo.model.User;
import com.example.demo.service.MyAccessTokenService;
import com.example.demo.service.UserService;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public User register(User entity) throws Exception{
		if(StringUtils.isEmpty(entity.getUsername()) || StringUtils.isEmpty(entity.getUserAccount())) {
			throw new Exception("账号和姓名不能为空");
		}
		User user = userMapper.selectByPrimaryKey(entity.getUserAccount());
		if(user != null) {
			throw new Exception("账号被占用");
		}
		entity.setId(UUID.randomUUID().toString().replace("-", ""));
		entity.setPassword("123456");
		entity.setIsDelete("0");
		userMapper.insert(entity);
		return entity;
	}

	@Override
	public void saveOrUpdate(User entity) throws Exception {
		if(StringUtils.isEmpty(entity.getId())) {
			register(entity);
		}else {
			userMapper.updateByPrimaryKeySelective(entity);
		}
		
	}

	@Override
	public void deleteLogic(Object key) throws Exception {
		User user = userMapper.selectByPrimaryKey(key);
		if(user == null) {
			throw new Exception("数据不存在");
		}
		if("admin".equals(user.getUserAccount())) {
			throw new Exception("不能删除admin用户");
		}
		user.setIsDelete("1");
		userMapper.updateByPrimaryKeySelective(user);
	}

}
